package com.example.fengyungo.controller.user.driver;

import com.example.fengyungo.constant.DriverConstant;
import com.example.fengyungo.constant.MailConstant;
import com.example.fengyungo.constant.MsgConstant;
import com.example.fengyungo.domain.pojo.DriverVerify;
import com.example.fengyungo.domain.pojo.User;
import com.example.fengyungo.result.Result;
import com.example.fengyungo.service.DriverService;
import com.example.fengyungo.service.DriverVerifyService;
import com.example.fengyungo.service.MailService;
import com.example.fengyungo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.Set;

@RestController
@RequestMapping("/user/driver")
public class DriverVerifyController {

    @Autowired
    private DriverService driverService;

    @Autowired
    private MailService mailService;

    @Autowired
    private DriverVerifyService driverVerifyService;


    /**
     * 发送验证码到司机邮箱
     *
     * @param email 邮箱
     * @return 验证码
     */
    @GetMapping("/send/{email}")
    public Result<Boolean> sendEmail(@PathVariable String email) {
        Boolean send = mailService.sendCodeMailToDriver(email);
        return Result.success(send);
    }

    /**
     * 对验证码进行验证
     *
     * @param email 邮箱
     * @param code  验证码
     * @return 验证结果
     */
    @GetMapping("/{email}/{code}")
    public Result<Boolean> verificationCode(@PathVariable String email, @PathVariable String code) {
        String identity = MailConstant.DRIVER_IDENTITY;
        Boolean verification = mailService.verificationCode(identity, email, code);
        return Result.success(verification);
    }


    /**
     * 提交司机认证信息
     *
     * @param driverVerify DriverVerify
     * @return Result
     */
    @PostMapping("/addDriverVerify")
    public Result addDriverVerify(@RequestBody DriverVerify driverVerify) {
        Integer res = driverVerifyService.addDriverVerify(driverVerify);
        if (res > 0) {
            // 修改司机状态为审核中
            Integer integer = driverService.updateDriverStatusById(driverVerify.getDriverId(), DriverConstant.VERIFYING);
            if (integer > 0) {
                return Result.success();
            } else {
                return Result.error(MsgConstant.DRIVER_INFO_UPDATE_FAIL);
            }
        } else {
            return Result.error(MsgConstant.DRIVER_VERIFY_SUBMIT_FAIL);
        }
    }
}
